import Taro from '@tarojs/taro'
import { observable, action, computed } from "mobx";
import $http from "../utils/http";


export default class DetailStore {
  @observable detailData: any = ''
  @observable tool_id: string = ''
  @observable check_user: string = ''
  @observable check_date: string = ''

  @computed
  get btnOn() {
    return !this.check_user ||
    !this.check_date
  }

  @action
  clear() {
    this.detailData = ''
    this.tool_id = ''
    this.check_user = ''
    this.check_date = ''
  }

  // 获取详情
  @action
  async getDetail(url: string): Promise<any> {
    Taro.showLoading({ title: '加载中...', mask: true })
    const result = await $http
      .post(url)
      .catch(() => Taro.hideLoading())
    Taro.hideLoading()
    if (result) {
      const { code, rows, msg } = result
      if (code === 100) {
        this.detailData = rows
        this.tool_id = rows.id
      } else {
        Taro.showToast({title: msg, icon: 'none'})
      }
    } else {
      Taro.showToast({title: '详情请求失败', icon: 'none'})
    }
  }

  @action
  async submit(params) {
    Taro.showLoading({ title: '正在提交...', mask: true })
    const result = await $http
      .post('http://project.com/index/tool/add_check', {
        tool_id: this.tool_id,
        check_user: this.check_user,
        check_date: this.check_date,
        ...params
      })
      .catch(() => Taro.hideLoading())
    Taro.hideLoading()
    if (result) {
      console.log(result)
    } else {
      Taro.showToast({title: '提交失败', icon: 'none'})
    }
  }

}
